package com.LeetCode.Array;

import org.junit.Test;

/**
 * 将数组往右挪 K 个位置
 */
public class RotateArray {

    public void rotate(int[] arr, int k) {
        int len = arr.length-1;
        k %= len;

        reverse(arr,0, len);
        reverse(arr, 0, k-1);
        reverse(arr, k, len);
    }

    public void reverse(int[] arr, int l, int r){
        while(l<r){
            int tmp = arr[l];
            arr[l++] = arr[r];
            arr[r--] = tmp;
        }
    }

    @Test
    public void test(){
        int[] arr = {1,2,3,4,5,6,7};
        rotate(arr,3);
        for(int num : arr){
            System.out.print(num + " ");
        }
    }
}
